<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>后台管理</title>
    <link rel="icon"
          href="${pageContext.request.contextPath}/lib/static/login/images/favicon.ico">
    <link rel="stylesheet"
          href="${pageContext.request.contextPath}/lib/layui/css/layui.css">
    <script
            src="${pageContext.request.contextPath}/lib/static/common/js/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/lib/layui/layui.js"
            charset="utf-8"></script>
    <script
            src="${pageContext.request.contextPath}/lib/static/common/js/jquery.form.js"></script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <fieldset class="layui-elem-field layui-field-title">

        </fieldset>
    </div>
    <hr class="layui-bg-red">
</div>
<table class="layui-hidden" id="listTable" lay-filter="listTable"></table><input type="hidden" id="id" name="id">
<div class="layui-btn-block layui-col-sm6 layui-col-sm-offset5">
    <button id="save" class="layui-btn" data-type="getCheckData"·>保存</button>
    <button id="reset" type="reset" class="layui-btn layui-btn-primary"
            onclick="window.location.href='${pageContext.request.contextPath}/sys/role/role_accredit'">重置</button>
</div>
<script>
    var editObj=null,ptable=null,dltable=null,tableId='listTable',layer=null;
    layui.config({
        base: '${pageContext.request.contextPath}/lib/layui/extend/'
    }).extend({
        dltable:'dltable' }).use(['jquery','dltable','layer']
        , function(){
            var $=layui.jquery;
            treeGrid = layui.treeGrid;//很重要
            layer=layui.layer;
            ptable =treeGrid.render({
                id:tableId
                ,elem: '#'+tableId
                ,idField:'id'
                ,url:'${pageContext.request.contextPath}/sys/menu/getAll'
                ,cellMinWidth: 100
                ,treeId:'id'//树形id字段名称
                ,treeUpId:'parentId'//树形父id字段名称
                ,treeShowName:'name'//以树形式显示的字段
                ,heightRemove:[".dHead",10]//不计算的高度,表格设定的是固定高度，此项不生效
                ,height:'full-150'
                ,isFilter:false
                ,iconOpen:true//是否显示图标【默认显示】
                ,isOpenDefault:false//节点默认是展开还是折叠【默认展开】
                ,loading:true
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'id', title:'菜单ID', width:80, fixed: 'left', unresize: true}
                    ,{field:'name',title:'菜单名称'}
                ]]
                ,isPage:false
            });
        });
            $(function () {
                $(".layui-btn-block")
                    .on("click","#save",accredit)
                layer.iframeAuto(index);
            })
            function accredit() {
                var checkStatus = treeGrid.checkStatus('listTable')
                data = checkStatus.data;
              /*layer.alert(JSON.stringify(data));*/
                var ids = [];
                $.each( data, function (index, value) {
                    var id = value.id;
                    ids.push(id);
                });
                var params = {
                    roleId:$("#id").val(),
                    menuId:ids.toString(),
                    isdel:0
                }
                /*layer.alert(ids.toString());*/
                var url = '${pageContext.request.contextPath}/sys/role/role_menu_add'
                if(data.length==0){
                    layer.alert('必须为角色分配一个资源', {
                        skin: 'layui-layer-molv'
                        , closeBtn: 0
                    });
                }else {
                    $.post(
                        url,
                        params,
                        function (result) {
                            if (result.code == 0) {
                                layer.msg(result.msg, {icon: 1, time: 500}, function () {
                                    parent.window.location.reload();
                                });
                            } else {
                                layer.msg(result.msg, {icon: 5, anim: 6});
                            }
                        },
                        "json"
                    );
                }
        }
</script>
</body>
</html>